/**
 * 当前是第几页
 */
var page2 = 1;
/**
 * 查询结果总页数
 */
var pageCountNum=7;
/**
 * 跳转修改图书操作页面
 * @param BookId 
 */
function doBookInfo(BookId) {
	$.ajax({
		type : 'POST',
		url : '../AdminServlet?method=bookinfo&bookId='+BookId,//返回admin-bookInfo.jsp页面
		cache : false,
		async : false,
		data : '',
		dataType : 'html',
		success : function(data) {
			$("#infos").html(data);						//显示图书信息到#infos
			creatPageBar2();							//显示分页
		},
		error: function() {
			alert("请求失败");
		}
	});
}

function test(){
	$.ajax({
		type : 'POST',
		url : '../AdminServlet?method=test',
		cache : false,
		async : false,
		data : '',
		dataType : 'json',
		success : function(data) {
			alert("请求成功");
		},
		error: function() {
			alert("请求失败");
		}
	});
}


/**
 * 获取图书列表
 * @param page 页数
 * @param categoryId 种类Id
 */
function doSearchBookList(page,categoryId){
	setCookie2("searchCategoryId", categoryId); 		//查询图书字段写入cookie
	page2=page;											//当前页数绑定到翻页
	setCookie2("pagenum", page);
	$.ajax({
		type : 'POST',
		url : '../AdminServlet?method=search&btype=categoryId&page='+page+
		'&pageCount=20&sort=desc&sorttype=id&fuzzy=false&name='+categoryId,//返回admin-bookInfo.jsp页面
		cache : false,
		async : false,
		data : '',
		dataType : 'html',
		success : function(data) {
			$("#infos").html(data);						//显示图书列表到#infos
			pageCountNum=$('#TotalNum').val();			//获取总页数
			creatPageBar2();							//显示分页
		},
		error: function() {
			alert("请求失败");
		}
	});
}

/**
 * 删除多本图书操作
 * @param formid 表单id
 */
function deleteBooks(formid) {
	var categoryId=getCookie2("searchCategoryId");		//获取图书种类
	page2=getCookie2("pagenum");						//获取当前页页数
	var fom = $("#" + formid);							//formid指定表单的id
	var inpt = fom.find("input:checked");				
	var valueArray = [];								
	inpt.each(function() {								
		var value = $(this).val();						//获取勾选图书的id
		valueArray.push(value);							
	});
	
	$.ajax({											
		type : 'POST',
														//返回admin-book.jsp页面,删除后需要刷新列表
		url : '../AdminServlet?method=bookdelete&btype=categoryId&page='+page2+
		'&pageCount=20&sort=desc&sorttype=id&fuzzy=false&name='+categoryId,
		cache : false,									
		async : false, 									
		data : "deleteId=" + JSON.stringify(valueArray),//将数组转化为JSON格式发送数据
		dataType : 'html',
		success : function(data) {
			$("#infos").html(data);						//显示图书列表到#infos
			pageCountNum=$('#TotalNum').val();			//获取总页数
			creatPageBar2();							//显示分页
		},
		error: function() {
			alert("请求失败");
		}
	});
}

/**
 * 删除单本图书操作
 * @param bookid 图书id
 */
function deleteBook(bookid) {
	var categoryId=getCookie2("searchCategoryId");		//获取图书种类
	var valueArray = [bookid];							//获取图书id，使用数组方便后边转化为json
	page2=getCookie2("pagenum");						//获取当前页页数
	$.ajax({											//请求删除操作
		type : 'POST',
		url : '../AdminServlet?method=bookdelete&btype=categoryId&page='+page2+
			'&pageCount=20&sort=desc&sorttype=id&fuzzy=false&name='+categoryId,//返回admin-book.jsp页面
		cache : false,									
		async : false,									
		data : "deleteId=" + JSON.stringify(valueArray),//转化为JSON格式发送数据
		dataType : 'html',
		success : function(data) {
			$("#infos").html(data);						//显示图书列表到#infos
			pageCountNum=$('#TotalNum').val();			//获取总页数
			creatPageBar2();							//显示分页
		},
		error: function() {
			alert("请求失败");
		}
	});
}


/**
 * 按种类查询图书
 * @param SelectId 选择框id
 * @param page 页数
 * @param count 查询记录条数
 */
function selectCategory(SelectId,page) {
	var select = $("#" + SelectId);
	var option = select.find("option:selected");
	var selectValue = option.val();						//获取下拉框种类值
	page2=page;											//改变当前页数
	setCookie2("pagenum", page);
	setCookie2("searchCategoryId", selectValue);		//图书种类写入cookie
	$.ajax({
		type : 'POST',
		url : '../AdminServlet?method=search&btype=categoryId&page='+ page+
		'&pageCount=20&sort=desc&sorttype=id&fuzzy=false&name='+ selectValue,
		cache : false,
		async : false,
		data : '',
		dataType : 'html',
		success : function(data) {
			$("#infos").html(data);						//显示图书列表到#infos
			pageCountNum=$('#TotalNum').val();			//获取总页数
			creatPageBar2();							//显示分页
		},
		error: function() {
			alert("请求失败");
		}
	});
}

/**
 *  ========================分页js============================
 *  */
function creatPageBar2() {
	var RJPageBar = document.getElementById('RJ-pagebar');
	var mark = document.getElementById('mark');
	var pageCount = pageCountNum;
	var singleCount = 20;
	if(pageCount==0){
		pageCount=1;
	}
	RJPageBar.innerHTML = "";
	RJ_Pagebar2({
		id : 'RJ-pagebar', 						// 分页容器ID
		CurrentPage : page2, 					// 当前为第几页
		SingleCount : singleCount, 				// 单页显示记录条数条数
		PageCount : pageCount, 					// 总页数
		callback : function(pagenow, pagecount) {
			mark.innerHTML = '当前页:' + pagenow + ',总共页:' + pagecount;
		}
	})
}
function RJ_Pagebar2(opt) {
	if (!opt.id) {
		return false
	};
	if (!opt.PageCount) {
		return false
	};
	var _obj = document.getElementById(opt.id);
	var _cp = parseInt(opt.CurrentPage) > parseInt(opt.PageCount) ? 1
			: parseInt(opt.CurrentPage) || 1;
	var _sc = parseInt(opt.SingleCount) > parseInt(opt.PageCount) ? 7
			: parseInt(opt.SingleCount) || 7;
	var _pc = parseInt(opt.PageCount);
	if (_sc % 2 == 0) {
		_sc = _sc - 1
	};
	var callback = opt.callback || function() {
	};

	if (_cp != 1) {
		var of = document.createElement('a');
		of.href = "#" + (1);
		of.innerHTML = "首页";
		_obj.appendChild(of);
		var oA = document.createElement('a');
		oA.href = "#" + (_cp - 1);
		oA.innerHTML = "上一页";
		_obj.appendChild(oA);
	} else {
		var of = document.createElement('span');
		of.className = "RU-pagedisabled";
		of.innerHTML = "首页";
		_obj.appendChild(of);
		var oS = document.createElement('span');
		oS.className = "RU-pagedisabled";
		oS.innerHTML = "上一页";
		_obj.appendChild(oS);
	}

	if (_cp <= (_sc - 1) / 2) {
		for (i = 1; i <= _sc; i++) {
			if (i == _cp) {
				var oS = document.createElement('span');
				oS.className = 'RU-pagenow';
				oS.innerHTML = i.toString().length == 1 ? "0" + i : i;
				_obj.appendChild(oS);
			} else {
				var oA = document.createElement('a');
				oA.href = "#" + i;
				oA.innerHTML = i.toString().length == 1 ? "0" + i : i;
				_obj.appendChild(oA);
			}
		}
		var oS = document.createElement('span');
		oS.innerHTML = "…";
		_obj.appendChild(oS);
	} else if (_cp <= _pc && _cp >= _pc - (_sc - 1) / 2) {
		var oS = document.createElement('span');
		oS.innerHTML = "…";
		_obj.appendChild(oS);
		for (i = _pc - _sc + 1; i <= _pc; i++) {
			if (i == _cp) {
				var oS = document.createElement('span');
				oS.className = 'RU-pagenow';
				oS.innerHTML = i.toString().length == 1 ? "0" + i : i;
				_obj.appendChild(oS);
			} else {
				var oA = document.createElement('a');
				oA.href = "#" + i;
				oA.innerHTML = i.toString().length == 1 ? "0" + i : i;
				_obj.appendChild(oA);
			}
		}
	} else {
		var oS = document.createElement('span');
		oS.innerHTML = "…";
		_obj.appendChild(oS);

		for (i = _cp - (_sc - 1) / 2; i < (parseInt(_cp) + parseInt(_sc) - (_sc - 1) / 2); i++) {
			if (i == _cp) {
				var oS = document.createElement('span');
				oS.className = 'RU-pagenow';
				oS.innerHTML = i.toString().length == 1 ? "0" + i : i;
				_obj.appendChild(oS);
			} else {
				var oA = document.createElement('a');
				oA.href = "#" + i;
				oA.innerHTML = i.toString().length == 1 ? "0" + i : i;
				_obj.appendChild(oA);
			}
		}
		var oS = document.createElement('span');
		oS.innerHTML = "…";
		_obj.appendChild(oS);
	}

	if (_cp != _pc) {
		var oA = document.createElement('a');
		oA.href = "#" + (_cp + 1);
		oA.innerHTML = "下一页";
		_obj.appendChild(oA);
		var oe = document.createElement('a');
		oe.href = "#" + (opt.PageCount);
		oe.innerHTML = "末页";
		_obj.appendChild(oe);
	} else {
		var oS = document.createElement('span');
		oS.className = "RU-pagedisabled";
		oS.innerHTML = "下一页";
		_obj.appendChild(oS);
		var of = document.createElement('span');
		of.className = "RU-pagedisabled";
		of.innerHTML = "末页";
		_obj.appendChild(of);
	}

	callback(_cp, _pc);

	var cA = _obj.getElementsByTagName('a');
	for (var i = 0; i < cA.length; i++) {
		cA[i].onclick = function() {
			var pagenum=$(this).html();					//获取当前点击a标签文本
			switch (pagenum) {
			case '上一页':
				pagenum=Number(getCookie2("pagenum", pagenum))-1;
				break;
			case '下一页':
				pagenum=Number(getCookie2("pagenum", pagenum))+1;
				break;
			case '首页':
				pagenum=1;
				break;
			case '末页':
				pagenum=$('#TotalNum').val();
				break;
			default:
				pagenum=pagenum.substring(1);
				break;
			}
			setCookie2("pagenum", pagenum);				//将当前页码保存cookie，供修改删除用
			if(getCookie2("searchCategoryId")==0){
				doSearchBookList(pagenum,getCookie2("searchCategoryId"));
			}else{
				doSearchBookList(pagenum,getCookie2("searchCategoryId"));
			}
			return false;
		}
	}
}

// =======================cookie===============================
/**
 * 写cookies
 */
function setCookie2(name, value) {
	var Days = 30;
	var exp = new Date();
	exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
	document.cookie = name + "=" + escape(value) + ";expires="
			+ exp.toGMTString();
}

/**
 * 读取cookies
 */
function getCookie2(name) {
	var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
	if (arr = document.cookie.match(reg))
		return unescape(arr[2]);
	else
		return null;
}
/**
 * 删除cookies
 */
function delCookie2(name) {
	var exp = new Date();
	exp.setTime(exp.getTime() - 1);
	var cval = getCookie2(name);
	if (cval != null)
		document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
}